###########################################################################
## Attributions of Fault and Support for Government Assistance for Workers
## Displaced by Technology
###########################################################################
## Seth Werfel
## Christopher Witko
## Tobias Heinrich
###########################################################################
## 1) Prepare the data
###########################################################################

## Load data, recodings
data <- read.dta13("data/data.DTA")

## Recode outcomes/ mediator
data$Treat <- mapvalues(x=data$condition,
                        from=paste0("SHW201_rand = ", 1:3),
                        to=c("Domestic", "Foreign", "Tech"))
data$Treat <- as.factor(as.character(data$Treat))

data$Support <- mapvalues(x=data$SHW201,
                          from=c("Strongly support", "Support", "Somewhat support",
                                 "Neither support nor oppose",
                                 "Somewhat oppose", "Oppose", "Strongly oppose"),
                          to=7:1)
data$Support <- as.numeric(as.character(data$Support))
data$SHW201 <- NULL


data$WFault <- mapvalues(x=data$SHW202,
                         from=c("Strongly agree", "Agree", "Somewhat agree",
                                "Neither agree nor disagree",
                                "Somewhat disagree", "Disagree", "Strongly disagree"),
                         to=7:1)
data$WFault <- as.numeric(as.character(data$WFault))
data$SHW202 <- NULL

data$Unemployed <- ifelse(data$employ == "Unemployed", 1, 0)

data$Age <- 2017 - data$birthyr

data$NewsInt_high <- ifelse(data$newsint %in% c("Most of the time", "Some of the time"), 1, 0)

data$Gender_male <- ifelse(data$gender == "Male", 1, 0)

data$VoterReg_yes <- ifelse(data$votereg == "Yes", 1, 0)

data$FamilyInc <- "Middle"
data$FamilyInc[data$faminc %in% c("Less than $10,000", "$10,000 - $19,999",
                                                "$20,000 - $29,999", "$30,000 - $39,999",
                                                "$40,000 - $49,999")] <- "Low"
data$FamilyInc[data$faminc %in% c("$150,000 or more", "$500,000 or more",
                                  "$350,000 - $499,999", "$250,000 - $349,999",
                                  "$200,000 - $249,999", "$150,000 - $199,999",
                                  "$120,000 - $149,999", "$100,000 - $119,999")] <- "High"
data$FamilyInc[data$faminc == "Prefer not to say"] <- "DKNO"

data$Race_white <- ifelse(data$race == "White", 1, 0)
data$Race_hispanic <- ifelse(data$race == "Hispanic", 1, 0)
data$Race_black <- ifelse(data$race == "Black", 1, 0)

data$Education_uni <- ifelse(data$educ %in% c("4-year", "Post-grad"), 1, 0)

data$Employed_full <- ifelse(data$employ == "Full-time", 1, 0)

data$Ideology <- mapvalues(x=data$ideo5,
                           from=c("Very liberal", "Liberal", "Moderate", "Conservative", "Very conservative", "Not sure",
                                  "skipped not asked"),
                           to=c(1:5, -99, -99))
data$Ideology <- as.numeric(as.character(data$Ideology))

data$Ideology_liberal <- ifelse(data$Ideology %in% c(1, 2), 1, 0)
data$Ideology_conservative <- ifelse(data$Ideology %in% c(4, 5), 1, 0)

data$pid3 <- as.factor(as.character(data$pid3))

data <- data[, c("FamilyInc", "VoterReg_yes", "Gender_male", "NewsInt_high",
                 "pid3", paste0("Race_", c("white", "hispanic", "black")), "Education_uni",
                 "Employed_full", "Ideology_liberal", "Ideology_conservative", 
                 "Age", "Unemployed", "WFault", "Support", "weight", "Treat")]

data$WFault <- as.factor(data$WFault)
data$Support  <- as.factor(data$Support)


## Drop one observation
data <- subset(data, is.na(Support) == FALSE)


## Save to disk
###############
save(data, file="output/Data_prepped.Rdata")



## Garbage collection
#####################
rm(list=ls())